﻿using System;
using System.Linq;
using T.FAS.Runtime.Base.Entity;
using T.FAS.Runtime.Metadata.Entity.DataModel;
using T.FAS.Runtime.Metadata.Client;
using T.FAS.Runtime.Metadata.SPI;
using T.FAS.Runtime.StateMachine.Service;
using T.FAS.Runtime.DataAccess.SPI;
using T.FAS.Runtime.DataAccess.Service;
using System.Collections.Generic;
using T.FAS.Runtime.DataAccess.SGSPI;
using T.ServiceComponent.Kernel.Service;

namespace T.FAS.Runtime.DataAccess.SGPlugin
{
    [SGService]
    public class DataAccessSGService : IDataAccessSGService
    {
        private static IMetaDataClientService metaDataClientService = MetaDataClientService.GetInstance();

        public string GetDQActionSQL(string dqId)
        {
            return DataQueryService.GetInstance().GetDQActionSQL(dqId);
        }

        public QueryPlan RepairQueryPlan(QueryPlan queryPlan)
        {
            return DataQueryService.GetInstance().RepairQueryPlan(queryPlan);
        }

        public TResult GetDOQueryResult(string id, Dictionary<string, object> parameters, Dictionary<string, object> exrParams = null)
        {
            DataObject doObj = metaDataClientService.GetMetaData(id).Content as DataObject;
            if (doObj == null)
                throw new Exception(string.Format("运行时获取DO:{0}实例失败", id));
            if (parameters == null) 
                parameters = new Dictionary<string, object>();
            return UnifiedDataAccessServiceFactory.GetInstance().QueryDataForDataObject(doObj, parameters, exrParams);
        }
    }
}
